<?php

// No direct access to this file
defined('_JEXEC') or die('Restricted access');
JTable::addIncludePath(JPATH_COMPONENT.DIRECTORY_SEPARATOR.'tables');

class KetnoiModelMaloi extends JModelLegacy
{
	public function getTable($type = 'Maloi', $prefix = 'KetnoiTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}
	
	function loadGridData($limitstart,$limit,$where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("*");
        $query->from("#__maloi");
        $query .= " limit $limitstart,$limit";  
        $db->setQuery($query);
        $result = $db->loadObjectList();
        return $result; 
    }
    
	function loadAllGridData($where)
    {
        $db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__maloi");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result; 
    }
    
	function save()
    {
    	global $app;
    	$result = array("success"=>true,"error"=>"");
    	
    	$arr_upload = Utility::uploadImages("file");
    	if(!$arr_upload["success"])
    	{
    		return $arr_upload;
    	}else 
    	{
    		$old_file = JRequest::getVar("old_file","");
    		if(!empty($old_file))
    		{
    			$old_file = json_decode($old_file,true);
    			$delete_file = json_decode(JRequest::getVar("delete_file"),true);
    			$img_dir = JPATH_SITE.DIRECTORY_SEPARATOR.'images'.DIRECTORY_SEPARATOR.'fileupload'.DIRECTORY_SEPARATOR;
    			
    			foreach ($old_file as $file)
    			{
    				if(!in_array($file, $delete_file))
    				{
    					$arr_upload["data"][] = $file;
    				}else 
    				{
    					if(JFile::exists($img_dir.$file))
    					{
    						JFile::delete($img_dir.$file);
    					}
    				}
    			}
    		}
    		$images = json_encode($arr_upload["data"]);
    		$table = $this->getTable();
	    	$post = JRequest::get("post");
	    	$post["hinhanh"] = $images;
			$table = Utility::save($table, $post);
			$result["id"] = $table->id;
			$result["ma"] = $table->ma;
			$result["ten"] = $table->ten;
    	}
    	return $result;
    }
    
    function loadData($id)
    {
    	$table = $this->getTable();
    	$table->load($id);
    	return $table;
    }
    
	function checkMaExists()
    {
    	$ma = JRequest::getVar("ma");
    	$oldval = JRequest::getVar("oldval","");
    	if(strtolower($ma) == strtolower($oldval))
    	{
    		return false;
    	}
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__maloi");
        $query->where("is_delete = 0 and ma = '$ma'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
	function checkTenExists()
    {
    	$ten = JRequest::getVar("ten");
    	$oldval = JRequest::getVar("oldval","");
    	if(strtolower($ten) == strtolower($oldval))
    	{
    		return false;
    	}
    	$db = JFactory::getDbo();
        $query = $db->getQuery(true);
        $query->select("count(*)");
        $query->from("#__maloi");
        $query->where("is_delete = 0 and ten = '$ten'");
        $db->setQuery($query);
        $result = $db->loadResult();
        return $result > 0;
    }
    
	function delete()
	{
		$id = JRequest::getVar("id");
		$db = JFactory::getDbo();
		$query = "update #__maloi set is_delete = 1 where id = $id";
		$db->setQuery($query);
		$db->query();
	}
	
}
